//
//  ScrollViewViewController.m
//  ssss
//
//  Created by 周成龙 on 17/5/9.
//  Copyright © 2017年 ZCL. All rights reserved.
//

#import "ScrollViewViewController.h"

#import "ItemView.h"

#define kWidth [UIScreen mainScreen].bounds.size.width
#define kHeight [UIScreen mainScreen].bounds.size.height

@interface ScrollViewViewController ()<UIScrollViewDelegate> {
    UIScrollView *_scrollView;
    NSArray *_imageNames;
    
    NSInteger _currentPage;
}

@end

@implementation ScrollViewViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _scrollView = [[UIScrollView alloc] init];
    _scrollView.frame = CGRectMake(0, 0, kWidth, kHeight);
    _scrollView.backgroundColor = [UIColor redColor];
    _scrollView.delegate = self;
    _scrollView.pagingEnabled = YES;
    _scrollView.bounces = NO;
    [self.view addSubview:_scrollView];
    _scrollView.contentSize = CGSizeMake(kWidth *3, kHeight);
    
    _imageNames = @[@"0.jpg", @"1.jpg", @"2.jpg", @"3.jpg", @"4.jpg", @"5.jpg"];
    
    for(int i = 0; i < 3; i ++) {
        ItemView *item = [[ItemView alloc] initWithFrame:CGRectMake(kWidth * i, 0, kWidth, kHeight)];
        item.image = [UIImage imageNamed:_imageNames[i]];
        //        item.image = [UIImage imageNamed:_imageNames[_currentPage]];
        item.userInteractionEnabled = YES;
        [_scrollView addSubview:item];
        [_scrollView setContentOffset:CGPointMake(kWidth, 0)];
    }
    
    _currentPage = 1;
    _scrollView.zoomScale = 1.0;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    CGFloat offSetX = scrollView.contentOffset.x;
    NSInteger index = offSetX / kWidth;
    
    //    NSLog(@"index:%zd", index);
    
    BOOL scrollToLeft = scrollView.contentOffset.x < kWidth;
    BOOL scrollToRight = scrollView.contentOffset.x > kWidth;
    
    //    if (scrollToLeft ) {
    //        NSLog(@"scrollToLeft");
    //    }else if(scrollToRight ) {
    //        NSLog(@"scrollToRight");
    //    }else {
    //        NSLog(@"--none--");
    //    }
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    CGFloat offSetX = scrollView.contentOffset.x;
    NSInteger index = offSetX / kWidth;
    
    // 向左还是向右滑动
    BOOL scrollToLeft = scrollView.contentOffset.x < kWidth;
    BOOL scrollToRight = scrollView.contentOffset.x > kWidth;
    
    if (scrollToLeft) {
        
        // 前一张
        ItemView *proItem = _scrollView.subviews[index];
        // 要出现的 view
        ItemView *item = _scrollView.subviews[index+1];
        // 后一张
        ItemView *backItem = _scrollView.subviews[index+2];
        
        for (UIView *view in item.subviews) {
            if ([view isKindOfClass:[UIScrollView class]]) {
                UIScrollView *scr = (UIScrollView *)view;
                [scr setZoomScale:1.0];
            }
        }
        
        // 设置该显示的图片的名称
        
        _currentPage -= 1;
        if (_currentPage < 0) {
            _currentPage = _imageNames.count-1;
        }
        if(_currentPage == 0) {
            proItem.image = [UIImage imageNamed:_imageNames[_imageNames.count-1]];
        }else {
            proItem.image = [UIImage imageNamed:_imageNames[_currentPage-1]];
        }
        
        item.image = [UIImage imageNamed:_imageNames[_currentPage]];
        
        if (_currentPage == _imageNames.count - 1) {
            backItem.image = [UIImage imageNamed:_imageNames[0]];
        }else {
            backItem.image = [UIImage imageNamed:_imageNames[_currentPage+1]];
        }
        
    }else if (scrollToRight) {
        // 前一张
        ItemView *proItem = _scrollView.subviews[index-2];
        // 要出现的 view
        ItemView *item = _scrollView.subviews[index-1];
        // 后一张
        ItemView *backItem = _scrollView.subviews[index];
        
        for (UIView *view in item.subviews) {
            if ([view isKindOfClass:[UIScrollView class]]) {
                UIScrollView *scr = (UIScrollView *)view;
                [scr setZoomScale:1.0];
            }
        }
        
        // 设置该显示的图片的名称
        _currentPage += 1;
        NSInteger page = _currentPage % _imageNames.count;
        if (page == _imageNames.count-1) {
            backItem.image = [UIImage imageNamed:_imageNames[0]];
        } else {
            backItem.image = [UIImage imageNamed:_imageNames[page + 1]];
        }
        
        if (page == 0) {
            proItem.image = [UIImage imageNamed:_imageNames[_imageNames.count-1]];
        }else {
            proItem.image = [UIImage imageNamed:_imageNames[page-1]];
        }
        
        if (_currentPage > _imageNames.count-1) {
            _currentPage = 0;
        }
        item.image = [UIImage imageNamed:_imageNames[_currentPage]];
    }else {
        
    }
    
    
    [_scrollView setContentOffset:CGPointMake(kWidth, 0)];
}


@end
